// pages/wordcloud/wordcloud.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths: [],
    url:"",
    wordcloud:[],
    current: 0,
    text: "",
    specificText: "",
    disableToNextStep: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  handleClick (e) {  //下一步 上一步的处理
    const id = e.target.dataset.id;
    if(id === 'prev'){
      const addCurrent = this.data.current - 1;
      var current = addCurrent < 0 ? 0 :addCurrent;
    }
    else{
      if(this.data.current === 2 && this.data.tempFilePaths.length === 0){
          wx.showToast({
            title: "请选择图片",
            icon: "warn"
          })
          return;
      }
      const addCurrent = this.data.current + 1;
      var current = addCurrent > 3 ? 0 : addCurrent;
    }
    this.setData({
        'current' : current
    })
  },
  getFullText(e){
      const text = e.detail.value;
      if(text.trim()!=""){
        this.setData({
          text: text,
          disableToNextStep: false,
          wordcloud: ""
        })
      }
      else{ // 如果光标离开 得到的文本是空，则让下一步按钮失效
        this.setData({
          disableToNextStep: true,
          wordcloud: ""
        })
      }
  },
  getSpecificText(e){
      const text = e.detail.value;
      this.setData({
        specificText: text,
        wordcloud: ""
      })
      console.log(this.data.specificText)
  },
  chooseimage: function () {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log("res", res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        _this.setData({
          tempFilePaths: res.tempFilePaths,
          wordcloud: ""
        })
        wx.request({
          url: res.tempFilePaths[0],
          method: 'GET',
          responseType: 'arraybuffer',
          success: function (res) {
            var base64 = wx.arrayBufferToBase64(res.data);
            _this.setData({
              url: 'data:image/jpg;base64,' + base64
            })
            // console.log(_this.data.url);
          }
        });
      }
    })
  },
  bindSubmit(e){
    const text = this.data.text;
    const specifiedWords = this.data.specificText;
    const mask = this.data.url;
    const that = this;
    wx.showLoading({
      title: "生成中请稍后"
    })
    wx.request({
      url: 'http://127.0.0.1:8888/api/nlp/genwordcloud',
      method: "POST",
      header: {
        'content-type': 'application/json'
      },
      data: {
        maskBackgroundImageBase64: mask,
        text: text,
        specifiedWords: specifiedWords
      },
      success: function(data){
        wx.hideLoading();
        that.setData({
          wordcloud: [data.data.data]
        })
      },
      fail: function(e){
        console.log(e)
        // wx.showToast({

        // })
      }
    })
  },
  previewImage: function (e) {
    console.log(e)
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接 
      urls: this.data.wordcloud // 需要预览的图片http链接列表 
    })
  },
})